package com.ruoyi.js.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.js.domain.JsPlayer;
import com.ruoyi.js.service.IJsPlayerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 选手Controller
 * 
 * @author greejoy
 * @date 2024-04-22
 */
@Controller
@RequestMapping("/js/player")
public class JsPlayerController extends BaseController
{
    private String prefix = "js/player";

    @Autowired
    private IJsPlayerService jsPlayerService;

    @RequiresPermissions("js:player:view")
    @GetMapping()
    public String player()
    {
        return prefix + "/player";
    }

    /**
     * 查询选手列表
     */
    @RequiresPermissions("js:player:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JsPlayer jsPlayer)
    {
        startPage();
        List<JsPlayer> list = jsPlayerService.selectJsPlayerList(jsPlayer);
        return getDataTable(list);
    }

    /**
     * 导出选手列表
     */
    @RequiresPermissions("js:player:export")
    @Log(title = "选手", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JsPlayer jsPlayer)
    {
        List<JsPlayer> list = jsPlayerService.selectJsPlayerList(jsPlayer);
        ExcelUtil<JsPlayer> util = new ExcelUtil<JsPlayer>(JsPlayer.class);
        return util.exportExcel(list, "player");
    }

    /**
     * 新增选手
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存选手
     */
    @RequiresPermissions("js:player:add")
    @Log(title = "选手", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JsPlayer jsPlayer, MultipartFile photoFile)
    {
        buildPhotoImage(jsPlayer, photoFile);
        return toAjax(jsPlayerService.insertJsPlayer(jsPlayer));
    }

    /**
     * 修改选手
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JsPlayer jsPlayer = jsPlayerService.selectJsPlayerById(id);
        mmap.put("jsPlayer", jsPlayer);
        return prefix + "/edit";
    }

    /**
     * 修改保存选手
     */
    @RequiresPermissions("js:player:edit")
    @Log(title = "选手", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsPlayer jsPlayer, MultipartFile photoFile)
    {
        buildPhotoImage(jsPlayer, photoFile);
        return toAjax(jsPlayerService.updateJsPlayer(jsPlayer));
    }

    /**
     * 删除选手
     */
    @RequiresPermissions("js:player:remove")
    @Log(title = "选手", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jsPlayerService.deleteJsPlayerByIds(ids));
    }


    private void buildPhotoImage(JsPlayer player, MultipartFile photoFile){
        // 上传文件路径
        String filePath = Global.getUploadPath();
        if(photoFile != null){
            String photo = null;
            try {
                photo = FileUploadUtils.upload(filePath, photoFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.setPhoto(photo);
        }
    }

}
